package com.skplanet.oneplusonewindow.moim;

import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class LoginActivity extends Activity {

	ImageButton profile;
	Bitmap profilebit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		Button button = (Button)findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
		
			public void onClick(View v) {
				EditText edit = (EditText)findViewById(R.id.editText1);
				user.getInstance().setname(edit.getText().toString());
				EditText edit2 = (EditText)findViewById(R.id.editText2);
				user.getInstance().setMiom_id(Integer.valueOf(edit2.getText().toString()));
				
	    		user.getInstance().setProfileImg(profilebit);
				Intent i = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(i);
			}
		});
		
		this.profile = (ImageButton)findViewById(R.id.profileImg);
		profile.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
				photoPickerIntent.setType("image/*");
				startActivityForResult(photoPickerIntent, 1);  
			}
		});
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
	    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

	    switch(requestCode) { 
	    case 1:
	        if(resultCode == RESULT_OK){  
	            Uri selectedImage = imageReturnedIntent.getData();
				try {
		            InputStream imageStream = null;
					imageStream = getContentResolver().openInputStream(selectedImage);
		            Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
		            Matrix matrix = new Matrix(); 
		    		matrix.postRotate(90); 
		    		int height = 70;
		    		double per = (double)height/(double)bitmap.getHeight();
		    		double width = (double)bitmap.getWidth()*per;
		    		Log.e("checksize", "test" + bitmap.getWidth());
		    		bitmap = Bitmap.createScaledBitmap(bitmap, (int)width, height, true);
		    		bitmap = Bitmap.createBitmap(bitmap, 0, 0, (int)width, height, matrix, true);
		    		profile.setImageBitmap(bitmap);
		    		profilebit = bitmap;
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	    }
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	

}
